<template>
  <div class="table-box">
    <div class="table-search" v-if="!isStats">
      <el-form :inline="true" :model="form">
        <el-form-item label="商户ID">
          <el-input v-model="form.merchant_id" placeholder="商户ID" />
        </el-form-item>
        <el-form-item label="币种">
          <el-input v-model="form.currency" placeholder="币种" />
        </el-form-item>
        <el-form-item label="交易类型">
          <!--<el-input v-model="form.trade_type" placeholder="交易类型" />-->
          <el-select clearable v-model="form.trade_type">
            <el-option
              v-for="item in tradeType"
              :key="item.value"
              :label="item.name"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="交易时间">
          <el-date-picker
            v-model="form.timeData"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="截止日期"
            :default-time="pickerDefaultTime"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onQuery">查询</el-button>
          <el-button @click="onReset">重置</el-button>
          <el-button type="primary" plain @click="onExport">导出</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="content-box" :class="!isStats ? 'card' : ''">
      <div class="table">
        <el-table :data="tableData" border v-loading="loading">
          <el-table-column prop="id" label="id" align="center" />
          <el-table-column
            prop="merchant_id"
            label="商户id"
            align="center"
            width="120"
          />
          <el-table-column
            prop="merchant_name"
            label="商户名称"
            align="center"
            width="200"
          />
          <el-table-column
            prop="createtime"
            label="交易时间"
            :formatter="formatterTime"
            align="center"
            width="180"
          ></el-table-column>
          <el-table-column
            prop="trade_type"
            label="交易类型"
            align="center"
            width="120"
          />
          <el-table-column prop="currency" label="币种" align="center" />
          <el-table-column
            prop="amount"
            label="交易金额"
            align="center"
            width="130"
          >
            <template #default="scope"
              >{{ scope.row.sg }}
              {{ numberToCurrencyNo(scope.row.amount) }}</template
            >
          </el-table-column>
          <el-table-column
            prop="amount_before"
            label="交易前金额"
            align="center"
            width="150"
          >
            <template #default="scope">{{
              numberToCurrencyNo(scope.row.amount_before)
            }}</template>
          </el-table-column>
          <el-table-column
            prop="amount_after"
            label="交易后金额"
            align="center"
            width="150"
          >
            <template #default="scope">{{
              numberToCurrencyNo(scope.row.amount_after)
            }}</template>
          </el-table-column>
          <el-table-column
            prop="freeze_before"
            label="冻结前金额"
            align="center"
            width="150"
          >
            <template #default="scope">{{
              numberToCurrencyNo(scope.row.freeze_before)
            }}</template>
          </el-table-column>
          <el-table-column
            prop="freeze_after"
            label="冻结后金额"
            align="center"
            width="150"
          >
            <template #default="scope">{{
              numberToCurrencyNo(scope.row.freeze_after)
            }}</template>
          </el-table-column>
          <el-table-column prop="record_id" label="源记录ID" align="center" />
          <el-table-column
            prop="description"
            label="备注"
            align="center"
            width="180"
          />
        </el-table>
        <div class="table-pagination">
          <el-pagination
            :current-page="pageable.pageNum"
            :page-size="pageable.pageSize"
            :page-sizes="[10, 25, 50, 100]"
            :background="true"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pageable.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          ></el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, reactive } from "vue";
import {
  formatterTime,
  pickerDefaultTime,
  timestamp,
  exportOrDownload,
  numberToCurrencyNo,
} from "@/utils/utils";
import {
  payMerAssetsRecordsApi,
  payMerAssetStatsInfoApi,
} from "@/api/ExAssistant/payassets";
const props = defineProps({
  isStats: {
    type: Boolean,
    default: false,
  },
  type: {
    type: Number,
    default: 0,
  },
  searchData: {
    type: Object,
    default: () => {},
  },
});
const loading = ref(false);
const tableData = ref([]);
const tradeType = ref([]);
const btnType = ref(null);
const formData = {
  merchant_id: "",
  currency: "",
  trade_type: "",
  timeData: [],
};
const form = reactive({ ...formData });
const pageable = reactive({
  pageNum: 1,
  pageSize: 10,
  total: 0,
});

// 参数
const param = () => {
  return {
    page: pageable.pageNum,
    pagesize: pageable.pageSize,
    merchant_id: form.merchant_id,
    currency: form.currency,
    trade_type: form.trade_type,
    start: form.timeData[0] ? timestamp(form.timeData[0].valueOf()) : "",
    end: form.timeData[1] ? timestamp(form.timeData[1].valueOf()) : "",
  };
};

// 获取表格数据
const getTbaleData = async () => {
  loading.value = true;
  try {
    let res = null;
    if (props.isStats) {
      let query = {
        ...props.searchData,
        page: pageable.pageNum,
        pagesize: pageable.pageSize,
      };
      query.type = 0;
      res = await payMerAssetStatsInfoApi(query);
    } else {
      res = await payMerAssetsRecordsApi(param());
    }

    loading.value = false;
    tableData.value = res.data.results;
    pageable.total = res.data.count;
    tradeType.value = res.data.extra.trade_type_map;
  } catch (error) {
    loading.value = false;
  }
};
// 查询
const onQuery = () => {
  //    console.log("onQuery");
  getTbaleData();
};
// 重置
const onReset = () => {
  Object.assign(form, formData);
  getTbaleData();
};
// 导出
const onExport = async () => {
  const data = Object.assign(param(), { export: 1, pagesize: 9999999 });
  const res = await payMerAssetsRecordsApi(data, "blob");
  exportOrDownload(res, "PayFaz-资产流水记录");
};
// 条数
const handleSizeChange = (number) => {
  pageable.pageSize = number;
  getTbaleData();
};
// 页码
const handleCurrentChange = (number) => {
  pageable.pageNum = number;
  getTbaleData();
};
onMounted(() => {
  getTbaleData();
});
</script>
